package com.anzhen.interview.lang;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import org.apache.commons.io.FileUtils;

import javax.annotation.Nullable;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Test {
    public static void main(String[] args) throws IOException {
        String str = FileUtils.readFileToString(new File("/Users/zhenan/Desktop/index.json"), "UTF8");
        Root root = JSON.parseObject(str, Root.class);

        List<Indexes> indexes = root.getIndexes();
        System.out.println("初始size");
        for (Indexes index : indexes) {
            System.out.println(index.getIndexDatas().size());
        }
        long start = System.currentTimeMillis();
        Set<String> allkeys = new HashSet<>();
        List<Set<String>> setList = Lists.transform(indexes, new Function<Indexes, Set<String>>() {
            @Override
            public Set<String> apply(Indexes indexes) {
                List<String> transform = Lists.transform(indexes.getIndexDatas(), new Function<IndexDatas, String>() {
                    @Override
                    public String apply(IndexDatas indexDatas) {
                        return indexDatas.getFd();
                    }
                });
                return new HashSet<>(transform);
            }
        });
        for (Set<String> strings : setList) {
            allkeys.addAll(strings);
        }
        System.out.println("allkeys" + allkeys.size());
        HashMap<String, Integer> keyIndex = new HashMap<>();
        ArrayList<String> keysList = Lists.newArrayList(allkeys);
        for (int i = 0; i < keysList.size(); i++) {
            keyIndex.put(keysList.get(i), i);
        }
        Set<String> keys = keyIndex.keySet();
        for (Indexes index : indexes) {
            List<IndexDatas> indexDatasList = index.getIndexDatas();
            if (indexDatasList.size() == keyIndex.size()) {
                continue;
            }
            List<String> copykeys = Lists.newArrayList(keys);
            LinkedList<IndexDatas> linkedList = new LinkedList<>();
            linkedList.addAll(indexDatasList);
            List<String> singleList = Lists.transform(indexDatasList, new Function<IndexDatas, String>() {
                @Override
                public String apply(IndexDatas indexDatas) {
                    return indexDatas.getFd();
                }
            });
            copykeys.removeAll(singleList);
            for (String copykey : copykeys) {
                IndexDatas indexDatas1 = new IndexDatas();
                indexDatas1.setFd(copykey);
                linkedList.add(keyIndex.get(copykey), indexDatas1);
            }
            index.setIndexDatas(Lists.newArrayList(linkedList));
        }
        System.out.println("耗时:" + (System.currentTimeMillis() - start) + "毫秒");
        System.out.println("完事size");
        for (Indexes index : indexes) {
            System.out.println(index.getIndexDatas().size());
        }
    }

}
